/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.swe.chums.beans;

import com.swe.chums.dao.ActivityTypeDao;
import com.swe.chums.dao.UserDao;
import com.swe.chums.dao.WebResourceDao;
import com.swe.chums.dao.exceptions.ActivityTypeDaoException;
import com.swe.chums.dao.exceptions.FeedbackDaoException;
import com.swe.chums.dao.exceptions.UserActivityDaoException;
import com.swe.chums.dao.exceptions.UserDaoException;
import com.swe.chums.dao.exceptions.WebResourceDaoException;
import com.swe.chums.dao.factory.DaoFactory;
import com.swe.chums.dao.model.Feedback;
import com.swe.chums.dao.model.UserActivity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.apache.log4j.Logger;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.event.data.FilterEvent;

/**
 *
 * @author cilingir
 */
@ManagedBean(name = "userActivityBean")
@ViewScoped
public class UserActivityBean implements Serializable{
    
    private static Logger log = Logger.getLogger(UserActivityBean.class);
    private ArrayList<UserActivity> myActivities,allActivities;
    
    public UserActivityBean() {
        myActivities = retrieveMyActivities();
        allActivities = retrieveAllActivities();
    }

   

    /**
     * retrieve my activities
     * 
     * @return 
     */
    public ArrayList<UserActivity> retrieveMyActivities() {
        log.debug("[UserActivityBean.retrieveMyActivities] - started");
        FacesContext context = FacesContext.getCurrentInstance();
        String errorText = "HATA";
        myActivities = new ArrayList<UserActivity>();
        try {
            LoginBean loginBean = (LoginBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginBean");
            if (loginBean != null && loginBean.isIsUser()) {
                myActivities = (ArrayList<UserActivity>) DaoFactory.createUserActivityDao().findWhereUserIdEquals(loginBean.getUser().getId());
                
                ActivityTypeDao activityTypeDao = DaoFactory.createActivityTypeDao();
                WebResourceDao webResourceDao = DaoFactory.createWebResourceDao();
                
                for (int i=0; i<myActivities.size(); i++) {
                    myActivities.get(i).setUserText(loginBean.getUser().getName());
                    myActivities.get(i).setActivityTypeText(activityTypeDao.findWhereIdEquals(myActivities.get(i).getActivityTypeId()).get(0).getName());
                    if (myActivities.get(i).getWebResourceId() != 0) {
                        myActivities.get(i).setWebResourceText(webResourceDao.findWhereIdEquals(myActivities.get(i).getWebResourceId()).get(0).getTitle());
                    }
                }
            }
        } catch (WebResourceDaoException ex) {
            log.error("[UserActivityBean.retrieveMyActivities] - error while retrieving values from DB, message is:  " + ex.getMessage());
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, errorText, "General DB Error"));
        } catch (ActivityTypeDaoException ex) {
            log.error("[UserActivityBean.retrieveMyActivities] - error while retrieving values from DB, message is:  " + ex.getMessage());
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, errorText, "General DB Error"));
        } catch (UserActivityDaoException ex) {
            log.error("[UserActivityBean.retrieveMyActivities] - error while retrieving values from DB, message is:  " + ex.getMessage());
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, errorText, "General DB Error"));
        }
        return myActivities;
    }
    
    /**
     * retrieve all user activities
     * @return 
     */
    public ArrayList<UserActivity> retrieveAllActivities() {
        log.debug("[UserActivityBean.retrieveAllActivities] - started");
        FacesContext context = FacesContext.getCurrentInstance();
        String errorText = "HATA";
        allActivities = new ArrayList<UserActivity>();
        try {
            LoginBean loginBean = (LoginBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginBean");
            if (loginBean != null && loginBean.isIsAdmin()) {
                allActivities = (ArrayList<UserActivity>) DaoFactory.createUserActivityDao().findAll();
                
                ActivityTypeDao activityTypeDao = DaoFactory.createActivityTypeDao();
                WebResourceDao webResourceDao = DaoFactory.createWebResourceDao();
                UserDao userDao = DaoFactory.createUserDao();
                
                for (int i=0; i<allActivities.size(); i++) {
                    allActivities.get(i).setUserText(userDao.findWhereIdEquals(allActivities.get(i).getUserId()).get(0).getName());
                    allActivities.get(i).setActivityTypeText(activityTypeDao.findWhereIdEquals(allActivities.get(i).getActivityTypeId()).get(0).getName());
                    if (allActivities.get(i).getWebResourceId() != 0) {
                        allActivities.get(i).setWebResourceText(webResourceDao.findWhereIdEquals(allActivities.get(i).getWebResourceId()).get(0).getTitle());   
                    }
                    
                }
            }
        } catch (UserDaoException ex) {
            log.error("[UserActivityBean.retrieveAllActivities] - error while retrieving values from DB, message is:  " + ex.getMessage());
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, errorText, "General DB Error"));
        } catch (WebResourceDaoException ex) {
            log.error("[UserActivityBean.retrieveAllActivities] - error while retrieving values from DB, message is:  " + ex.getMessage());
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, errorText, "General DB Error"));
        } catch (ActivityTypeDaoException ex) {
            log.error("[UserActivityBean.retrieveAllActivities] - error while retrieving values from DB, message is:  " + ex.getMessage());
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, errorText, "General DB Error"));
        } catch (UserActivityDaoException ex) {
            log.error("[UserActivityBean.retrieveAllActivities] - error while retrieving values from DB, message is:  " + ex.getMessage());
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, errorText, "General DB Error"));
        }
        return allActivities;
    }
    
    
    /**
     * Remove activity
     * 
     */
    public void removeAnyActivity() {
        
    }
    
    /**
     * Filter event handler
     *
     * @param event
     * @return
     */
    public Map<String, String> onFilter(FilterEvent event) {

        log.debug("[AdminBean.onFilterAllWebResources] started");
        Map<String, String> filters;
        DataTable table = (DataTable) event.getSource();

//        List<User> obj = table.getFilteredData();
//        if (obj != null) {
//            table.getFilteredData().remove(obj);
//        }
        filters = table.getFilters();
        filters.clear();

        return filters;
    }
    
    
    /*----Getter Setters---- */

    public ArrayList<UserActivity> getMyActivities() {
        return myActivities;
    }

    public void setMyActivities(ArrayList<UserActivity> myActivities) {
        this.myActivities = myActivities;
    }

    public ArrayList<UserActivity> getAllActivities() {
        return allActivities;
    }

    public void setAllActivities(ArrayList<UserActivity> allActivities) {
        this.allActivities = allActivities;
    }

    
   
}
